<?php

namespace api\controller;

use common\controller\VerifyController;
use common\model\TaskModel;
use common\exception\LogicException;
use common\service\TaskMemberService;
use common\validate\TemplateTodo as TemplateTodoValidate;
use common\validate\TaskMember as TaskMemberValidate;
use support\ErrorCode;
use think\Exception;
use think\Request;
use think\Response;

class TaskMemberController extends VerifyController
{
    private $taskMemberService;

    public function __construct()
    {
        parent::__construct();
        $this->taskMemberService = TaskMemberService::getInstance();
    }


    /**
     * 创建待办任务
     * @param Request $request
     * @return Response
     * @throws \Throwable
     */
    public function create(Request $request)
    {
        $data = $request->param()['data'];
        $resData = $this->taskMemberService->create($data, $request->getUserId(), $request->getTenantId());
        return json(success_response('', $resData));
    }

    /**
     * 查询待办任务
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function select(Request $request)
    {
        $param = $request->param();
        $resData = $this->taskMemberService->select($param['param'], $request->getUserId(), $request->getTenantId());
        return json(success_response('', $resData));
    }

    /**
     * 查询待办各个状态的总数
     * @return Response
     * @throws \Exception
     */
    public function selectStatusTotal(Request $request)
    {
        $resData = $this->taskMemberService->selectStatusTotal($request->getUserId(), $request->getTenantId());
        return json(success_response('', $resData));
    }

    /**
     * 更新待办任务
     * @param Request $request
     * @return Response
     * @throws \Throwable
     * @throws \think\Exception
     */
    public function updateTask(Request $request)
    {
        $param = $request->param();
        try {
            $resData = $this->taskMemberService->updateTask($param['data'], $request->getUserId(), true);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 更新待办任务状态
     * @param Request $request
     * @return Response
     */
    public function updateStatus(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->taskMemberService->updateStatus($param['data'], $request->getUserId());
            return json(success_response('', $resData));
        } catch (\Throwable $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查找指定任务
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function find(Request $request)
    {
        $param = $request->param();
        $resData = $this->taskMemberService->find($param['param'], $request->getUserId());
        return json(success_response('', $resData));
    }

    /**
     * 查找任务成员
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getTaskMemberList(Request $request)
    {
        $param = $request->param();

        $resData = $this->taskMemberService->getTaskMemberList($param['param'] ?? []);
        return json(success_response('', $resData));
    }

    /**
     * 新增待办成员
     * @param Request $request
     * @return Response
     */
    public function addMember(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->taskMemberService->addMember($param['data']);
            return json(success_response('', $resData));
        } catch (\Throwable $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 删除待办成员
     * @param Request $request
     * @return Response|void
     */
    public function removeMember(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->taskMemberService->removeMember($param['param']);
            return json(success_response('', $resData));
        } catch (\Throwable $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查找工单待办人员
     * @param Request $request
     * @return Response
     */
    public function selectWorkOrderUser(Request $request)
    {
        $param = $request->param();
        $resData = $this->taskMemberService->selectWorkOrderUser($param['param']['filter']['work_order_id']);
        return json(success_response('', $resData));
    }

    /**
     * 分配工单待办人员
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function allocationWorkOrderUser(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->taskMemberService->allocationWorkOrderUser($param['data']);
            return json(success_response('', $resData));
        } catch (\Throwable $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 分配工单待办人员
     * @param Request $request
     * @return Response
     * @throws \Throwable
     */
    public function allocationWorkOrderUserV2(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->taskMemberService->allocationWorkOrderUserV2($param['data']);
            return json(success_response('', $resData));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 用户任务设置
     * @param Request $request
     * @return Response|void
     */
    public function userTaskSet(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->taskMemberService->userTaskSet($param['data'], $request->getUserId());
            return json(success_response('', $resData));
        } catch (\Throwable $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取用户任务设置
     * @param Request $request
     * @return Response|void
     */
    public function getUserTaskSet(Request $request)
    {
        try {
            $resData = $this->taskMemberService->getUserTaskSet($request->getUserId(), $request->getTenantId());
            return json(success_response('', $resData));
        } catch (\Throwable $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 彻底删除待办任务
     * @param Request $request
     * @return Response
     * @throws Exception
     * @throws \Throwable
     */
    public function delete(Request $request)
    {
        $param = $request->param();
        $resData = $this->taskMemberService->deleteTaskMember($param['param']['filter'], $request->getUserId());
        return json(success_response('', $resData));
    }

    /**
     * 按类型统计任务的关联任务
     * @param Request $request
     * @return Response
     * @throws \think\Exception
     */
    public function taskRelationCountByRelationType(Request $request)
    {
        $param = $request->param();
        $resData = $this->taskMemberService->taskRelationCountByRelationType($param['param']);
        return json(success_response("OK", $resData));
    }

    /**
     * 查找任务关联项
     * @param Request $request
     * @return Response
     * @throws \think\Exception
     */
    public function taskRelationList(Request $request)
    {
        $param = $request->param();
        $resData = $this->taskMemberService->taskRelationList($param['param']);
        return json(success_response("OK", $resData));
    }

    /**
     * 批量创建待办
     * @param Request $request
     * @return Response|void
     */
    public function batchCreate(Request $request)
    {
        // 验证数据
        $paramData = $request->param()['data'];
        $validate = new TaskMemberValidate();
        foreach ($paramData as $validData) {
            if (!$validate->check($validData, null, 'BatchCreateValidate')) {
                throw_strack_exception($validate->getError(), ErrorCode::TASK_MEMBER_PARAM_ERR);
            }
        }

        // 组织批量创建数据
        $data = [];
        foreach ($paramData as $item) {
            $data[] = [
                'task' => ['step_type' => TaskModel::STEP_TYPE_TO_DO, 'description' => $item['description'], 'name' => $item['name'], 'plan_start_time' => $item['plan_start_time'], 'plan_end_time' => $item['plan_end_time']],
                'executor' => [$request->getUnionUserId()]
            ];
        }
        try {
            $resData = $this->taskMemberService->batchCreate($data);
            return json(success_response('', $resData));
        } catch (\Throwable $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 根据任务ID和交付文件类型ID查找版本历史
     * @param Request $request
     * @return Response
     */
    public function getDeliveryVersionHistory(Request $request)
    {
        $param = $request->param()['param'];
        $res = $this->taskMemberService->getDeliveryVersionHistory($param['filter'], $param['page']);
        return json(success_response('', $res));
    }


    /**
     * 添加工单成员
     * @param \think\Request $request
     * @return Response
     * @throws \Throwable
     */
    public function addWorkOrderUser(Request $request)
    {
        $data = $request->param()['data'];
        $res = $this->taskMemberService->addWorkOrderUser($data);
        return json(success_response('', $res));
    }

    /**
     * 删除工单成员
     * @param \think\Request $request
     * @return Response
     */
    public function deleteWorkOrderUser(Request $request)
    {
        $data = $request->param()['data'];
        $res = $this->taskMemberService->deleteWorkOrderUser($data);
        return json(success_response('', $res));
    }

}
